﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using JSC.Core;
using JSC.Portal.Services;
using JSC.Portal.Entities;
using JSC.Core.Data;
using NHibernate;
using JSC.Data.NHibernate;
using JSC.Portal.Web.Controls;
using JSC.Portal.Web.Filters;

namespace JSC.Portal.Web.Controllers
{
    [Authorize(Roles = "Administrator")]
    [HandleError]
    public class PortalSitesController : ServicedController<IPortalSiteService>
    {
        private IDao<Template> _templateDao;
        private IDao<ModuleType> _moduleTypeDao;
        public PortalSitesController()
        {
             _templateDao = new CommonDao<Template>();
             _moduleTypeDao = new CommonDao<ModuleType>();
        }

        public ActionResult List()
        {
            return View("List", Service.GetPortalSites());
        }

        public ActionResult Edit(int id)
        {
            PortalSite portalSite = Service.GetById(id, false);
            LoadSelectionLists(portalSite);
            return View("Edit", portalSite);
        }

        public ActionResult Create()
        {
            PortalSite portalSite = new PortalSite();
            LoadSelectionLists(portalSite);
            return View("Edit", portalSite);
        }

        public ActionResult Delete(int id)
        {
            using (ITransaction trans = NHibernateSession.Current.BeginTransaction())
            {
                PortalSite portalSite = Service.GetById(id, false);
                Service.Delete(portalSite);
                trans.Commit();
                return RedirectToAction("List");
            }
        }

        public ActionResult SaveOrUpdate(int id, string name, string description, string defaultLanguage, string url, Template defaultTemplate,
            string webmasterMail, string metaKeywords, string metaDescription, DateTime validfrom, DateTime? validtill, int[] selectedModules)
        {
            using (ITransaction trans = NHibernateSession.Current.BeginTransaction())
            {
                PortalSite ps = Service.GetById(id, false, true);
                ps.Name = name;
                ps.Description = description;
                ps.DefaultLanguage = defaultLanguage;
                ps.Url = url;
                ps.WebmasterMail = webmasterMail;
                ps.DefaultTemplate = _templateDao.GetById(defaultTemplate.Id, false);
                ps.MetaKeywords = metaKeywords;
                ps.MetaDescription = metaDescription;
                ps.ValidFrom = validfrom;
                ps.ValidTill = validtill;
                if (id < 0)
                    ps = Service.SaveOrUpdate(ps);
                ps.LicensedModules.Clear();
                foreach (int sm in selectedModules)
                {
                    SiteLicense sl = new SiteLicense();
                    sl.PortalSite = ps;
                    sl.LicensedModule = _moduleTypeDao.GetById(sm, false);
                    sl.Licensed = true;
                    ps.LicensedModules.Add(sl);
                }
                ps.SetTrackedData(User.Identity as PortalUser);
                ps = Service.SaveOrUpdate(ps);
                trans.Commit();
                return RedirectToAction("Edit", new { id = ps.Id });
            }
        }

        private void LoadSelectionLists(PortalSite portalSite)
        {
            IList<Template> templates = _templateDao.GetAll();
            this.ViewData["Templates"] = new SelectList(templates, "Id", "Name", portalSite.DefaultTemplate == null ? string.Empty : portalSite.DefaultTemplate.Id.ToString());
            this.ViewData["Languages"] = new SelectList(Language.GetLanguages(), "Culture", "Description", portalSite.DefaultLanguage);
            this.ViewData["SiteLicenses"] = GetLicenses(portalSite);            
        }

        private IList<SiteLicense> GetLicenses(PortalSite portalSite)
        {
            IList<SiteLicense> licenses = new List<SiteLicense>();
            IList<ModuleType> modules = _moduleTypeDao.GetAll();
            foreach (ModuleType m in modules)
            {
                SiteLicense s = new SiteLicense();
                s.PortalSite = portalSite;
                s.LicensedModule = m;
                if (portalSite != null)
                {
                    foreach (SiteLicense sl in portalSite.LicensedModules)
                    {
                        s.Licensed = (sl.Licensed && sl.LicensedModule.Id == m.Id);
                        if (s.Licensed)
                            break;
                    }
                }
                licenses.Add(s);
            }
            return licenses;
        }
    }
}
